import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { UserService } from '../../../service/userService';
import { HttpService } from '../../../service/httpService';
import { NativeService } from '../../../service/NativeService';
import { AlertController } from 'ionic-angular';
import { AddBankPage } from '../../me/bank/addBank';
import { HistoryPage } from '../history/history';
@Component({
  selector: 'page-withdrawal',
  templateUrl: 'withdrawal.html',
})
export class WithdrawalPage {
  private money:number;
  private banks:any;
  private bankId = -1;
  private withdrawalPwd = '';
  constructor(public navCtrl: NavController,
              public user:UserService,
              public http:HttpService,
              public nat:NativeService,
              public alert:AlertController,
              public navParams: NavParams) {
    this.initData();
  }

  initData(){
    let res = this.user.banks;

    if(!res || res.length <= 0){
      this.banks = [{
        name: 'banks',
        options: [{text:'无银行卡',value:-2}]
      }];
      let alert = this.alert.create({
        title:"无取款银行卡",
        subTitle:'是否立即添加银行卡',
        buttons:[
          {
            text: '稍后添加',
            handler: (value: any) => {
              this.navCtrl.pop();
            }
          },{
            text: '立即添加',
            handler: (value: any) => {
              this.navCtrl.push(AddBankPage);
            }
          }
        ]
      });
      alert.present();
      return;
    }
    let options = [];
    for(var i=0;i<res.length;i++){
      let item = res[i];
      let bankName = item.Bank.BankName;
      let account = item.AccountNo;
      let obj = {text:bankName,value:item.Id};
      if(account.length > 4){
       obj.text = bankName + ' (****' + account.substring(account.length - 4,account.length)+')';
      }
      if(item.DefaultCard){
        this.bankId = item.Id;
      }
      options.push(obj);
    }

    if(this.bankId == -1){
      this.bankId = res[0].Id;
    }
    this.banks = [{
      name: 'banks',
      options: options
    }];
  }

  gotoPageAction(){
    this.navCtrl.push(HistoryPage,{title:"取款记录"});
  }
  withdrawalAll(){
    this.money = this.user.cash;
  }

  withdrawalAction(){

    if (!this.money || this.money <= 0){
      this.nat.showToast('请输入取款金额');
      return;
    }
    if (this.withdrawalPwd == ''){
      this.nat.showToast('取款密码不能为空');
      return;
    }

    let url = this.http.baseUrl + '/api/Withdrawal/AddWithdrawal';
    let map = {
      UserName:this.user.userName,
      BankAccountId:this.bankId,
      Amount:this.money,
      Remark:'',
      WithdrawalPwd:this.withdrawalPwd
    };
    let self = this;
    this.nat.showLoading('申请取款中....');
    this.http.post(url,map,function (res,err) {
      self.nat.hideLoading();
      if(!err){
        if(res){
          self.user.cash -= self.money;
          self.nat.showToast('申请成功');
          self.navCtrl.pop();
        }
      }
    })
  }
}
